<?php

namespace App\Console\Commands;

use App\depository\SubTeamUserDep;
use Illuminate\Console\Command;
use App\depository\TeamsDep;
use Laravel\Sanctum\PersonalAccessToken;

class TradeCmd extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'trade:upd';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Execute daily task';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $dep = new TeamsDep();
        $subTeamUserDep = new SubTeamUserDep();
        $resDep = $dep->get();

        foreach ($resDep as $value) {
            $expire_at_timestamp = strtotime($value['expire_at']);
            $current_timestamp = strtotime(date("Y-m-d H:i:s"));
            if ($expire_at_timestamp <= $current_timestamp) {
                $dep->upd($value['id'], ['is_open' => 2]);
                $resSubTeamUser = $subTeamUserDep->getByTeamId($value['id']);
                $resSubTeamUser->each(function($item){
                    PersonalAccessToken::where('tokenable_id',$item['user_id'])->delete();
                });
                $subTeamUserDep->delByTeamId($value['id']);
            }
        }
        $this->info("处理完成, {}");
    }
}
